<template>
      <el-container class="container-layout">
        <el-aside :width="isOpen?'200px':'64px'">
            <!-- logo -->
            <div class="logo" :class="{'mini_logo':!isOpen}"></div>

            <!-- 左侧导航菜单 -->
            <el-menu router
                
                style="border-right:none"
                :collapse="!isOpen"
                :collapse-transition="false"
                :default-active="$route.path" 
                background-color="#002233"
                text-color="#fff"
                active-text-color="#ffd04b">
                <!--el-submenu 当有子菜单时使用该组件  -->
                <!-- el-menu-item 没有子菜单时使用 -->
                <el-menu-item index="/">
                    <!-- 图标 -->
                    <i class="el-icon-s-home"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">首页</span>
                </el-menu-item>
                <el-menu-item index="/article">
                    <!-- 图标 -->
                    <i class="el-icon-document"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">内容管理</span>
                </el-menu-item>
                <el-menu-item index="/image">
                    <!-- 图标 -->
                    <i class="el-icon-picture"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">素材管理</span>
                </el-menu-item>
                <el-menu-item index="/publish">
                    <!-- 图标 -->
                    <i class="el-icon-s-promotion"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">发布文章</span>
                </el-menu-item>
                <el-menu-item index="/comment">
                    <!-- 图标 -->
                    <i class="el-icon-chat-dot-round"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">评论管理</span>
                </el-menu-item>
                <el-menu-item index="/fans">
                    <!-- 图标 -->
                    <i class="el-icon-present"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">粉丝管理</span>
                </el-menu-item>
                <el-menu-item index="/setting">
                    <!-- 图标 -->
                    <i class="el-icon-setting"></i>
                    <!-- 菜单文字 -->
                    <span slot="title">个人设置</span>
                </el-menu-item>
                
            </el-menu>
        </el-aside>
        <el-container>
            <!-- 右侧头部区域 -->
            <el-header>
                <!-- 图标 -->
                <span 
                    @click="isOpen=!isOpen" 
                    class="icon" 
                    :class="{'el-icon-s-fold':isOpen,'el-icon-s-unfold':!isOpen }" >
                </span>
                <!-- 文字 -->
                <span class="text">江苏传值</span>
                <!-- 下拉菜单 -->
                <el-dropdown @command="clickItem">
                    <!-- 默认显示 -->
                    <span class="el-dropdown-link">
                        <!-- 头像 -->
                        <img class="head" :src="user.photo" alt="">
                        <!-- 用户名 -->
                        <span class="name">{{user.name}}</span>
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <!-- 下拉选项 -->
                    <el-dropdown-menu slot="dropdown">
                        <!-- 原因：click事件没触发，此时绑定的是组件，组件并未去支持click -->
                        <!-- 解决：把click事件绑定在组件最终解析的dom标签上，dom标签支持click -->
                        <!-- 实现：在事件后加上事件修饰符 .native 给组件根标签绑定原生事件
                        @click.native="setting()" @click.native="logout()"
                         -->
                        <el-dropdown-item command="setting"  icon="el-icon-setting">个人设置</el-dropdown-item>
                        <el-dropdown-item command="logout"  icon="el-icon-unlock">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </el-header>
            <el-main>
                <!-- 二级路由显示位置 -->
                <router-view></router-view>
            </el-main>
        </el-container>
      </el-container>
</template>

<script>
import  auth from '@/utils/auth'
import eventBus from '@/eventBus'
export default {
    name:'layout',
    data(){
        return {
            isOpen:true,
            //用户名称，用户头像
            user:{
                name:'',
                photo:''
            }
        }   
    },
    created() {
        const {name,photo} = auth.getUser()
        this.user = {name,photo}
        //绑定eventBus的初始化事件
        eventBus.$on('updateUserName',data=> {
            this.user.name = data
        })
        //头像
        eventBus.$on('updateUserPhoto',data=> {
            this.user.photo = data
        })
    },
    methods:{
        setting(){
            this.$router.push('/setting')
        },
        logout(){
            //1.清除用户信息
            //2.跳转登录页面
            auth.delUser()
            this.$router.push('/login')
        },
        clickItem(command){
            //commend=setting,调用上面setting函数
            //commend=logout,调用logout函数
            this[command]()
        }
    }
}
</script>

<style scoped lang="less">
.container-layout{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    .el-aside {
        background: #002233;
        .logo{
            width: 100%;
            height: 60px;
            background: #002244 url(../assets/images/logo_admin.png) no-repeat center/140px auto;
        }
        //小logo的样式需要在logo下方书写
        .mini_logo {
            background-image: url(../assets/images/logo_admin_01.png);
            background-size: 36px auto ;
        }
    }
    .el-header{
        border-bottom: 1px solid #dddddd;
        line-height: 60px;
        .icon {
            font-size: 24px;
            vertical-align: middle;
        }
        .text{
            vertical-align: middle;
            margin-left: 10px;
        }
        .el-dropdown{
           float: right;
            .head{
                width: 30px;
                height: 30px;
                vertical-align: middle;
            }
            .name{
                vertical-align: middle;
                margin-left: 10px;
            }
        }
    }
}
</style>